package es

import (
	"github.com/olivere/elastic"
)

var (
	url      = "http://127.0.0.1:9200"
	sniff    = false
	infoLog  = NewEsLog()
	traceLog = NewEsLog()
	errorLog = NewEsLog()
)

var esClient *elastic.Client

func InitEsDefault() {

}

func InitEs(urlTmp string) {
	url = urlTmp
}

func newEsClient() (*elastic.Client, error) {
	client, err := elastic.NewClient(
		elastic.SetSniff(sniff),
		elastic.SetURL(url),
		elastic.SetInfoLog(infoLog),
		elastic.SetErrorLog(errorLog),
		elastic.SetTraceLog(traceLog),
	)
	if err != nil {
		esFatalHandler(map[string]interface{}{
			"sniff": sniff,
			"url":   url,
		}, err)
	}
	return client, err
}

func GetEsClient() *elastic.Client {
	var err error
	if esClient == nil {
		esClient, err = newEsClient()
		if err != nil {
			panic(err)
		}
	}
	return esClient
}
